home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / libs / bestl232 / examp002.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-11  |  18.1 KB  |  385 lines

  1. /*==========================================================================
  2.  *
  3.  *  EXAMP002.C                                    Sunday, September 11, 1994
  4.  *
  5.  *  sample source code for The BESTLibrary, intended to give examples of:
  6.  *    txt_fade_in
  7.  *    txt_fade_out
  8.  *    txt_fill_area
  9.  *    txt_flood
  10.  *    txt_print
  11.  *    txt_set_act
  12.  *    txt_set_vis
  13.  *    txt_str_show
  14.  *
  15.  *  Authored independently by George Vanous
  16.  *
  17.  *==========================================================================*/
  18.  
  19.  
  20. /* ------------------------------------------------------------------------ */
  21. /* ----------------------------  INCLUDE FILES  --------------------------- */
  22.  
  23. #include <alloc.h>
  24. #include <stdlib.h>
  25. #include "!bestlib.h"                  /* include !BESTLIB.H in compilation */
  26.  
  27. /* ------------------------------------------------------------------------ */
  28. /* ------------------------------  CONSTANTS  ----------------------------- */
  29.  
  30. #define BGCLR BLUE                              /* background color         */
  31. #define TEXTHEIGHT 12                           /* y-height of printed text */
  32. #define BUFFERSPACE 100                         /* byte size of "buffer"    */
  33. #define PRINT(x, y, text) _16_boxfill(x, y+2, textwidth(text), TEXTHEIGHT-2, BLACK, COPY_IMAGE); outtextxy(x, y, text);
  34.  
  35. /* ------------------------------------------------------------------------ */
  36. /* ------------------------------  MESSAGES  ------------------------------ */
  37.  
  38. #define ERR01 "\nI require 65,536 bytes (64kb) of free memory\nBut there is only %ld bytes currently available\n\nDo you want me to continue regardless [y/N] ? "
  39. #define MSG01 "\n\nI suggest you try removing any unnecessary TSRs (Terminate but Stay Resident\nprograms) to increase the amount of available memory.\n\n"
  40. #define DASHES printf("\n--------------------------------------------------------------------------------")
  41.  
  42. /* ------------------------------------------------------------------------ */
  43. /* -------------------------  GLOBAL DEFINITIONS  ------------------------- */
  44.  
  45. /* The following three definition are required by TheBESTLibrary to be
  46.    present, even if they are not all used.  If they are not present, the
  47.    compiler will produce a "linker error". */
  48. asciiscan key;                         /* global structure "key"            */
  49. cursordata cursor;                     /* global structure "cursor"         */
  50. mousedata msdata;                      /* global structure "msdata"         */
  51.  
  52. filldata fidata;                       /* global structure "fidata"         */
  53. printdata prdata;                      /* global structure "prdata"         */
  54.  
  55. byte curx, cury,                       /* the cursor (x,y)                  */
  56.      oldmode;                          /* old video mode                    */
  57. char *buffer;                          /* general-purpose string buffer     */
  58.  
  59. /* ------------------------------------------------------------------------ */
  60. /* -------------------------  FUNCTION PROTOTYPES  ------------------------ */
  61.  
  62. void begin_sequence(void);                      /* beginning sequence       */
  63. void draw_menu(void);                           /* display main menu        */
  64. void exit_sequence(void);                       /* exitting sequence        */
  65. void mouse_demo(void);                          /* demonstrate mouse        */
  66. void floodtext(char *text[]);                   /* part of "mouse_demo"     */
  67. void showtext(char *text[]);                    /* part of "mouse_demo"     */
  68. void string_demo(void);                         /* demonstrate strings      */
  69. void text_animate(void);                        /* animates text            */
  70. void text_fade(void);                           /* demonstrate text fading  */
  71.  
  72. void (*menuselect[])(void) = {                  /* pointer to functions     */
  73.   text_fade, mouse_demo, string_demo, text_animate
  74. };
  75.  
  76. /* ------------------------------------------------------------------------ */
  77.  
  78.  
  79. /*----------------------------------------------------------------------------
  80.  * MAIN SUBROUTINE OF EXAMPLE SOURCE CODE 002
  81.  */
  82. void main(void)
  83. {
  84.   int choice,                          /* user's menu choice                */
  85.       count;
  86.  
  87.   begin_sequence();                    /* perform the beginning sequence    */
  88.   draw_menu();
  89.   txt_scroll_over(400);                /* scroll up the menu screen         */
  90.   txt_set_act(TEXTPAGE1);              /* set first text page as active     */
  91.   txt_set_vis(TEXTPAGEACTIVE);         /* show active text page             */
  92.   txt_split(1023);                     /* restore screens to normal         */
  93.  
  94.   while (TRUE) {
  95.     kbd_clear();                       /* clear keyboard buffer             */
  96.     count = txt_get_num(25, 14, &choice, 1, '∙', "Type in your selection: ",
  97.                         LIGHTCYAN, LIGHTBLUE, "1");
  98.     if (count != -1 && choice == 1)
  99.       (*menuselect[choice-1])();       /* perform the desired demonstration */
  100.     else
  101.       break;                           /* else exit program                 */
  102.     draw_menu();
  103.   };
  104.  
  105.   exit(0);  /* executes the procedure "exit_sequence()" because of the line
  106.                "atexit(exit_sequence)" in procedure "begin_sequence"        */
  107. }
  108.  
  109. /*----------------------------------------------------------------------------
  110.  * Beginning sequence.
  111.  */
  112. void begin_sequence(void)
  113. {
  114.   char ch;                             /* generic character holder          */
  115.   byte i, j;                           /* cursor position holders           */
  116.  
  117.   /* allocate space for a generic string buffer */
  118.   buffer = (char *) malloc(BUFFERSPACE);
  119.  
  120.   /* check amount of available memory */
  121.   DASHES;                              /* print one line of dashes          */
  122.   if (coreleft() < 64000L) {
  123.     sprintf(buffer, ERR01, coreleft());
  124.     fprintf(stderr, buffer);           /* print low memory warning message  */
  125.     cur_get_coord_abs(&i, &j);         /* get the cursor position           */
  126.     while ((ch = case_up(getchre(i, j))) != 'Y' && ch != 'N' && ch != 13) {
  127.       beep();                          /* signal invalid keypress           */
  128.       txt_chr_erase(i, j, 1);          /* erase character                   */
  129.     }
  130.     if (ch != 'Y') {                   /* if 'Y' chosen                     */
  131.       fprintf(stderr, MSG01);          /*   print suggestion message        */
  132.       exit(1);                         /*   exit to DOS with ERRORLEVEL 1   */
  133.     }                                  /* else continue with program        */
  134.   }
  135.  
  136.   /* initialize The BEST Library and save old video mode
  137.    * set 25-line text mode (TEXT25) and initialize the mouse (TRUE)         */
  138.   oldmode = bestlib_init(TEXT25, TRUE);
  139.   txt_set_act(TEXTPAGELAST);           /* set last text page as active      */
  140.   txt_mem(TEXTSHOW, NULL);             /* write DOS screen to text page 2   */
  141.   txt_set_vis(TEXTPAGEACTIVE);         /* show active text page             */
  142.   txt_set_act(TEXTPAGE1);              /* set first text page as active     */
  143.  
  144.   /* store that status of the insert, caps, num, and scroll lock */
  145.   kbd_status_save();
  146.  
  147.   ms_show();                           /* show mouse cursor                 */
  148.   txt_blink(FALSE);                    /* enable 16 background colors       */
  149.   cur_off();                           /* hide the cursor                   */
  150.   atexit(exit_sequence);               /* define the exit procedure         */
  151. //txt_fade_out();                      /* fade current text screen to black */
  152. }
  153.  
  154. /*----------------------------------------------------------------------------
  155.  * Display the main menu.
  156.  */
  157. void draw_menu(void)
  158. {
  159.   txt_flood(WHITE, BLUE, " ");         /* flood with white fg and blue bg   */
  160.   txt_str_show(0, 0, "MOUSE", ALIGN_NONE);
  161.   txt_str_show(80, 0, "STRING", ALIGN_HORZ);
  162.   txt_str_show(80, 0, "TEXT MODE", ALIGN_RIGHT);
  163.   txt_str_show(0, 2, "Demonstration", ALIGN_HORZ);
  164.   txt_str_show(2, 5, "1) Text fading", ALIGN_NONE);
  165.   txt_str_show(0, 11, "<ESC> Exit to DOS", ALIGN_NONE);
  166. }
  167.  
  168. /*----------------------------------------------------------------------------
  169.  * Exiting sequence.
  170.  */
  171. void exit_sequence(void)
  172. {
  173.   kbd_status_load();
  174.   ms_hide();                           /* hide mouse cursor                 */
  175. //txt_fade_out();                      /* fade original text screen back in */
  176.   txt_set_vis(TEXTPAGELAST);           /* show last text page               */
  177.   txt_scroll_over(-400);               /* restore original text screen      */
  178.   video_restore(oldmode);              /* restore original video status     */
  179. //txt_fade_in();                       /* fade in the text screen           */
  180.   exit(0);                             /* exit to DOS with ERRORLEVEL 0     */
  181. }
  182.  
  183. /*----------------------------------------------------------------------------
  184.  * Demonstrate mouse routines.
  185.  */
  186. void mouse_demo(void)
  187. {
  188. #define TXT01 0
  189. #define TXT02 1
  190. #define TXT03 2
  191. #define TXT04 3
  192. #define TXT05 4
  193. #define TXT06 5
  194. #define TXT07 6
  195. #define TXT08 7
  196. #define TXT09 8
  197. #define TXT0A 9
  198. #define TXT0B 10
  199. #define TXT0C 11
  200. #define LOC1 7
  201. #define LOC2 11
  202. #define LOC3 5
  203. #define LOC4 0
  204. #define LOC5 24
  205. #define QUE01 12
  206. #define QUE02 13
  207.  
  208.   char *text[] = {
  209.     "Demonstration of Microsoft compatible mouse routines",        /* TXT01 */
  210.     "Authored independently by George Vanous",                     /* TXT02 */
  211.     "left button is not pressed",                                  /* TXT03 */
  212.     "left button is pressed    ",                                  /* TXT04 */
  213.     "left button was pressed at",                                  /* TXT05 */
  214.     "left button was released at",                                 /* TXT06 */
  215.     "right button is not pressed",                                 /* TXT07 */
  216.     "right button is pressed    ",                                 /* TXT08 */
  217.     "right button was pressed at",                                 /* TXT09 */
  218.     "right button was released at",                                /* TXT0A */
  219.     " cursor is located at",                                       /* TXT0B */
  220.     "press any key to terminate",                                  /* TXT0C */
  221.     "Which ASCII character to use as cursor? [219] ",              /* QUE01 */
  222.     "What color? [7] "                                             /* QUE02 */
  223.   };
  224.   int chr, color;
  225.   shortint count;
  226.  
  227.   txt_flood(LIGHTGREY, BLACK, " ");    /* clear the screen and set colors   */
  228.   prdata.x = 55, prdata.y = 11;        /* set the next print position       */
  229.   count = txt_get_num(prdata.x, prdata.y, &chr, 3, ' ', text[QUE01],
  230.                  BLACK, WHITE, "219"); /* get the user's input              */
  231.   if (count == -1) return;             /* return to menu if <ESC> pressed   */
  232.   prdata.y += 2;                       /* set the next print position       */
  233.   count = txt_get_num(prdata.x, prdata.y, &color, 1, ' ', text[QUE02],
  234.                    BLACK, WHITE, "7"); /* get the user's input              */
  235.   if (count == -1) return;             /* return to menu if <ESC> pressed   */
  236.  
  237.   txt_flood(LIGHTGREY, BLACK, " ");    /* clear the screen and set colors   */
  238.   showtext(text);                      /* print up the screen               */
  239.   prdata.y = LOC1;                     /* define the y-coordinate to print  */
  240.   prdata.command = -1;                 /* specify no modifications          */
  241.   prdata.text = text[TXT03];           /* specify the string to print       */
  242.   txt_print(MEM, MEM, &prdata);        /* print left mouse button status    */
  243.   prdata.y = LOC2;                     /* define the y-coordinate to print  */
  244.   prdata.text = text[TXT07];           /* specify the string to print       */
  245.   txt_print(MEM, MEM, &prdata);        /* print right mouse button status   */
  246.   ms_shape_set(chr, color, -1);        /* change mouse cursor and color     */
  247.   stopw(0, 15);                        /* start stopwatch 0                 */
  248.  
  249.   fidata.fgclr = GREEN, fidata.bgclr = BLACK,
  250.   fidata.x = fidata.y = 0,
  251.   fidata.length = 80, fidata.height = 25,
  252.   fidata.text = NULL;
  253.   while (!kbd_hit()) {
  254. //  floodtext(text);                   /* fill the entire screen            */
  255.     ms_stat();                         /* read the mouse status             */
  256.     if (msdata.pos[0] != msdata.npos[0]) {
  257.       msdata.pos[0] = msdata.npos[0];
  258.       msdata.update += 1;              /* if the mouse x-position changed.. */
  259.     }
  260.     if (msdata.pos[1] != msdata.npos[1]) {
  261.       msdata.pos[1] = msdata.npos[1];
  262.       msdata.update += 2;              /* if the mouse y-position changed.. */
  263.     }
  264.     if (msdata.buts[0] != msdata.nbuts[0]) {
  265.       msdata.buts[0] = ~msdata.buts[0];
  266.       msdata.update += 4;      /* if the left mouse button state changed..  */
  267.     }
  268.     if (msdata.buts[1] != msdata.nbuts[1]) {
  269.       msdata.buts[1] = ~msdata.buts[1];
  270.       msdata.update += 8;      /* if the right mouse button state changed.. */
  271.     }
  272.     if (msdata.update) {               /* if mouse registered activity..    */
  273.       msdata.update = 0;               /* reset the mouse activity flag     */
  274.       prdata.fgclr = YELLOW;           /* set the foreground text color     */
  275.       prdata.command = 0;              /* specify text to be centered       */
  276.       prdata.x = 0, prdata.y = LOC3;   /* define the location to print      */
  277.       sprintf(prdata.text = buffer, "%s %d, %d  ",
  278.               text[TXT0B], msdata.pos[0], msdata.pos[1]);
  279.       txt_print(MEM, MEM, &prdata);    /* print cursor location             */
  280.       prdata.command = -1;             /* specify no modifications          */
  281.       prdata.y = LOC1;                 /* define y-coordinate of next print */
  282.       if (!msdata.buts[0]) {           /* if left button is not pressed..   */
  283.         prdata.text = text[TXT03];
  284.         txt_print(MEM, MEM, &prdata);  /* print left button not pressed     */
  285.         sprintf(prdata.text = buffer, "%s %d, %d  ",
  286.                 text[TXT06], msdata.butlr[0], msdata.butlr[1]);
  287.         prdata.y++;                    /* increment y-coordinate of print   */
  288.       }
  289.       else {                           /* else left button is pressed..     */
  290.         prdata.text = text[TXT04];
  291.         txt_print(MEM, MEM, &prdata);  /* print left button is pressed      */
  292.         sprintf(prdata.text = buffer, "%s %d, %d  ",
  293.                 text[TXT05], msdata.butlp[0], msdata.butlp[1]);
  294.         prdata.y += 2;                 /* add to y-coordinate of print      */
  295.       }
  296.       txt_print(MEM, MEM, &prdata);    /* print left button coordinates     */
  297.       prdata.y = LOC2;                 /* define new y-coordinate for print */
  298.       if (!msdata.buts[1]) {           /* if right button is not pressed..  */
  299.         prdata.text = text[TXT07];
  300.         txt_print(MEM, MEM, &prdata);  /* print right button unpressed      */
  301.         sprintf(prdata.text = buffer, "%s %d, %d  ",
  302.                 text[TXT0A], msdata.butrr[0], msdata.butrr[1]);
  303.         prdata.y++;                    /* increment y-coordinate of print   */
  304.       }
  305.       else {                           /* else right button is pressed..    */
  306.         prdata.text = text[TXT08];
  307.         txt_print(MEM, MEM, &prdata);  /* print right button is pressed     */
  308.         sprintf(prdata.text = buffer, "%s %d, %d  ",
  309.                 text[TXT09], msdata.butrp[0], msdata.butrp[1]);
  310.         prdata.y += 2;                 /* add to y-coordinate of print      */
  311.       }
  312.       txt_print(MEM, MEM, &prdata);    /* print right button coordinates    */
  313.     }
  314.   }
  315. }
  316.  
  317. /* 32 250 249 46 7 176 177 178 219
  318.  *     ·   ∙   .    ░   ▒   ▓   █  */
  319. void floodtext(char *text[])
  320. #define FLOODCHARNO 8
  321. {
  322.   static int cycle = 1, dir = 1;
  323.   int data[] = { 32, 250, 249, 46, 7, 176, 177, 178, 219 };
  324.  
  325.   if (stopw(0, 0) == TRUE) {
  326.     fidata.fillchar = data[cycle];     /* set the character to fill with    */
  327.     sprintf(buffer, "%c", data[cycle - dir]);
  328.     fidata.overwrite = buffer;         /* specify the string to overwrite   */
  329.     if (cycle == FLOODCHARNO || (cycle == 1 && dir < 0))
  330.       dir = -dir;                      /* cycle fill characters in reverse  */
  331.     cycle += dir;                      /* get the next fill character       */
  332.     txt_fill_area(MEM, MEM, &fidata);  /* fill screen with the character    */
  333.     msdata.update++;                   /* reprint the mouse status onscreen */
  334.     showtext(text);                    /* reprint the standard text         */
  335.     stopw(0, 15);                      /* reset stopwatch 0                 */
  336.   }
  337. }
  338.  
  339. /*----------------------------------------------------------------------------
  340.  * Print initial text.
  341.  */
  342. void showtext(char *text[])
  343. {
  344.   prdata.fgclr = WHITE, prdata.bgclr = BLUE;         /* set printing colors */
  345.   prdata.command = ALIGN_HORZ;         /* center the text to print          */
  346.   prdata.x = 0, prdata.y = LOC4;       /* define the coordinates to print   */
  347.   prdata.text = text[TXT01];           /* specify the string to print       */
  348.   txt_print(MEM, MEM, &prdata);        /* print first line of title         */
  349.   prdata.y++;                          /* increment the y-coordinate        */
  350.   prdata.text = text[TXT02];           /* specify the string to print       */
  351.   txt_print(MEM, MEM, &prdata);        /* print second line of title        */
  352.   prdata.y = LOC5;                     /* define the y-coordinate to print  */
  353.   prdata.text = text[TXT0C];           /* specify the string to print       */
  354.   txt_print(MEM, MEM, &prdata);        /* print the "any key exits" message */
  355. }
  356.  
  357. /*----------------------------------------------------------------------------
  358.  * Demonstrate string routines.
  359.  */
  360. void string_demo(void)
  361. {
  362.  
  363. }
  364.  
  365. /*----------------------------------------------------------------------------
  366.  * Animate text.
  367.  */
  368. void text_animate(void)
  369. {
  370.  
  371. }
  372.  
  373. /*----------------------------------------------------------------------------
  374.  * Demonstrate text fading.
  375.  */
  376. void text_fade(void)
  377. {
  378.   fade_out();                          /* fade current screen to black      */
  379.   fade_in();                           /* fade screen back in               */
  380. }
  381.  
  382. /*==========================================================================*/
  383.  
  384. // REALiTY
  385.